/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.janalise.util;

import br.com.janalise.model.MapaDoTexto;
import java.awt.Toolkit;
import java.io.File;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;

/**
 *
 * @author boskyn9
 */
public class Repositorio {

    public void getTreeModelFiles(JTree jTree,String repositorio,List<MapaDoTexto> textos){
        DefaultMutableTreeNode root = new DefaultMutableTreeNode(repositorio);


        File file = new File(repositorio);
        if(!file.exists())
            JOptionPane.showMessageDialog(null, "Diretório inexistente!", "Erro dir", JOptionPane.ERROR_MESSAGE);
        File filhos[] = file.listFiles();

        for (int i = 0; i < filhos.length; i++) {
            File file1 = filhos[i];
            if(!file1.isDirectory()){
                if(textos!=null)
                    for (MapaDoTexto texto : textos) {
                        if(texto.getPath().equals(file1.getAbsolutePath()))
                            addFilhos(root,file1);
                    }
                else
                    addFilhos(root,file1);
            }else{
                setNo(file1,textos,root);
            }
        }

        DefaultTreeModel model = new DefaultTreeModel(root);

        ImageIcon leafIcon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/imagens/filePDF.png")));
        ImageIcon closedIcon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/imagens/pastaFechada.png")));
        ImageIcon openIcon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/imagens/pasta.png")));




        DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer)jTree.getCellRenderer();
        renderer.setLeafIcon(leafIcon);
        renderer.setClosedIcon(closedIcon);
        renderer.setOpenIcon(openIcon);


        UIManager.put("Tree.leafIcon", leafIcon);
        UIManager.put("Tree.closedIcon", closedIcon);
        UIManager.put("Tree.openIcon", openIcon);

        jTree.setModel(model);
    }

    private void addFilhos(DefaultMutableTreeNode root,File file1) {
        if(isPDF(file1)){
                root.add(new DefaultMutableTreeNode(file1.getName()));
        }else if(file1.isDirectory()){
            File[] files = file1.listFiles();
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                addFilhos(root,file);
            }
        }
    }
    private boolean isPDF(File file){
        return(!file.isDirectory() && file.getName().toLowerCase().endsWith(".pdf"));
    }

    private void setNo(File file, List<MapaDoTexto> textos, DefaultMutableTreeNode root) {
        File filhos[] = file.listFiles();
        for (int i = 0; i < filhos.length; i++) {
            File file1 = filhos[i];
            if(!file1.isDirectory()){
                if(textos!=null)
                    for (MapaDoTexto texto : textos) {
                        if(texto.getPath().equals(file1.getAbsolutePath()))
                            addFilhos(root,file1);
                    }
                else
                    addFilhos(root,file1);
            }else{
                setNo(file1,textos,root);
            }
        }
    }

}
